package org.xu.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class URLCodecUtils {

    // 默认字符编码
    private static final String DEFAULT_ENCODING = "UTF-8";

    private static final String fileNameRegex = "[&#=/?+%]";

    /**
     * URL 编码
     *
     * @param input 要编码的字符串
     * @return 编码后的字符串
     * @throws UnsupportedEncodingException 如果指定的编码不受支持
     */
    public static String encode(String input) throws UnsupportedEncodingException {
        return URLEncoder.encode(input, DEFAULT_ENCODING);
    }

    /**
     * URL 解码
     *
     * @param input 要解码的字符串
     * @return 解码后的字符串
     * @throws UnsupportedEncodingException 如果指定的编码不受支持
     */
    public static String decode(String input) throws UnsupportedEncodingException {
        return URLDecoder.decode(input, DEFAULT_ENCODING);
    }

    public static void main(String[] args) {
            String fileName = "5-%E6%8A%B5%E6%8A%BC%E7%99%BB%E8%AE%B0%E8%AF%81&#40;%E9%A6%96%E9%A1%B5&#41;.jpg";
            fileName = fileName.replaceAll(fileNameRegex,"");
        System.out.println(fileName);
    }

}
